﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections;
using ComLib.ComType;

namespace ComLib
{
    //资源类，用于记录映射类型和设置 
    [StructLayout(LayoutKind.Sequential)]
    public class NETRESOURCE
    {
        public int dwScope;//只能取2 
        public int dwType;//0为打印机或驱动器，1为驱动器，2为打印机 
        public int dwDisplayType;//取0，自动设置 
        public int dwUsage;//取1 
        public string LocalName;//本地盘符或名称 
        public string RemoteName;//远程地址 
        public string Comment;//NULL即可，A pointer to a NULL-terminated string that contains a comment supplied by the network provider. 
        public string Provider;//NULL即可，A pointer to a NULL-terminated string that contains the name of the provider that owns the resource. This member can be NULL if the provider name is unknown. 
    }

    //控制(主)类，创建、删除映射 
    public class NetDriveCtl
    {
        ArrayList NDList;

        private string _strErrorInfo = "";
        public string ErrorInfo
        {
            get
            {
                return _strErrorInfo;
            }
        }

        public NetDriveCtl()
        {
            NDList = new ArrayList();
        }

        public bool Exists(string account, string password, string localName, string RemoteName)
        {
            NETRESOURCE NetDrive = new NETRESOURCE();
            NetDrive.dwScope = 2;
            NetDrive.dwType = 1;
            NetDrive.dwDisplayType = 3;
            NetDrive.dwUsage = 1;
            NetDrive.LocalName = localName;
            NetDrive.RemoteName = RemoteName;
            NDList.Add(NetDrive);
            string strRes = ConnectDrive(NetDrive, account, password);        
            return  strRes == "85";
        }

        public string CreateDrive(string account, string password, string localName, string RemoteName)
        {
            NETRESOURCE NetDrive = new NETRESOURCE();
            NetDrive.dwScope = 2;
            NetDrive.dwType = 1;
            NetDrive.dwDisplayType = 3;
            NetDrive.dwUsage = 1;
            NetDrive.LocalName = localName;
            NetDrive.RemoteName = RemoteName;

            NDList.Add(NetDrive);
            string strRes = ConnectDrive(NetDrive, account, password);
            if (strRes == "1219" || strRes == "85")
            {
                strRes = DisconnectDrive(NetDrive);
                strRes = ConnectDrive(NetDrive, account, password);
            }
            return strRes;
        }

        public Boolean DeleteDrive(string LocalName, string RemoteName)
        {
            foreach (NETRESOURCE NetDrive in NDList)
            {
                if ((NetDrive.LocalName == LocalName) && (NetDrive.RemoteName == RemoteName))
                {
                    DisconnectDrive(NetDrive);
                    NDList.Remove(NetDrive);
                    return true;
                }
            }
            return false;
        }

        private string ConnectDrive(NETRESOURCE NetDrive, string UserName, string Password)
        {
            StringBuilder UN = new StringBuilder(UserName);
            StringBuilder PW = new StringBuilder(Password);

            return WNetAddConnection2(NetDrive, PW, UN, 0).ToString();
        }

        private string DisconnectDrive(NETRESOURCE NetDrive)
        {
            string LocalName = NetDrive.LocalName;
            return WNetCancelConnection2(LocalName, 1, true).ToString();
        }

        private string DisconnectDrive(string LocalName)
        {
            return WNetCancelConnection2(LocalName, 1, true).ToString();
        }

        //这两个是系统API函数 
        [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
        private static extern uint WNetAddConnection2([In] NETRESOURCE lpNetResource, StringBuilder lpPassword, StringBuilder lpUsername, uint dwFlags);
        [DllImport("Mpr.dll")]
        private static extern uint WNetCancelConnection2(string lpName, uint dwFlags, bool fForce);
    } 
}
